package com.wink_172.library.utils

import android.app.Activity
import android.app.Dialog
import android.content.Context
import android.content.Intent
import android.graphics.PixelFormat
import android.os.Build
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.RelativeLayout
import android.widget.TextView
import com.opensource.svgaplayer.SVGAImageView
import com.wink_172.library.AppManager
import com.wink_172.library.LibApplication
import com.wink_172.library.R
import com.wink_172.library.callback.ICallback
import com.wink_172.library.model.Constants
import com.wink_172.library.utils.DisplayUtil
import com.wink_172.library.view.MySeekBar
import org.xutils.x


/**
 * DialogUtils
 * */
object DialogUtils {

    private const val TAG = "DialogUtils"


    /**
     * svga
     */
    fun showSvgaDialog(activity: Activity?, svga_name: String, callback: ICallback?) {
        val dialog = Dialog(activity!!, R.style.CustomProgressDialog)
        dialog.setContentView(R.layout.dialog_show_svga)
        dialog.setCanceledOnTouchOutside(false)
        dialog.setCancelable(false)
        dialog.window!!.attributes.gravity = Gravity.CENTER
        dialog.show()

        //      设置宽度全屏，要设置在show的后面
        if (true) {
            val layoutParams = dialog!!.window!!.attributes
            layoutParams.gravity = Gravity.CENTER
            layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT
            layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT
            dialog!!.window!!.decorView.setPadding(0, 0, 0, 0)
            dialog!!.window!!.attributes = layoutParams
        }

        val svga1 = dialog.findViewById<SVGAImageView>(R.id.svga1)
        CommonUtils.showSingleSVGAByName(
            LibApplication.svgaParser_full_screen,
            svga1,
            svga_name,

            object :
                com.wink_172.library.callback.ICallback {
                override fun onSendEvent(
                    event: Int,
                    vararg args: Any?
                ) {//这个callback用的非常巧妙    //这里因为不能传递非Serialiazable,对象 所以需要精简代码
                    if (event == Constants.EventEnum.svga动效播完了.ordinal) {
                        dialog.dismiss()
                    }

                }
            },
            true,
            false
        );
    }



    /**
     * 下载进度框
     * */
    fun showProgressDialog(activity: Activity): Dialog {
        val dialog = Dialog(activity, com.wink_172.library.R.style.dialog)
        dialog.setContentView(com.wink_172.library.R.layout.progress_dialog)
        dialog.setCanceledOnTouchOutside(true)
        dialog.setCancelable(true)
        dialog.show()

        val title = dialog.findViewById<TextView>(com.wink_172.library.R.id.title)
        val hint = dialog.findViewById<TextView>(com.wink_172.library.R.id.hint)
        val progress = dialog.findViewById<MySeekBar>(com.wink_172.library.R.id.progress)
//        title.setText("正在下载...")


//        val all_size= args[0] as Int
//        val current_size= args[1] as Int
//        val processPP=(current_size.toFloat()/all_size.toFloat()*100).toInt()
//
//
//        hint.setText("下载进度（ "+processPP+"/100 )")
//        progress.currentProgress=processPP
//        progress.maxProgress=100
//        progress.invalidate()

        return dialog
    }


    /**
     * 只展示标题不展示content
     * 展示普通通用弹窗  左右双按钮
     * str1标题
     * str2左按钮
     * str3右按钮
     */
    /**
     * 展示普通通用弹窗  左右双按钮
     * str0标题
     * str1内容
     * str2左按钮
     * str3右按钮
     */
    fun showCommonDialog2(
        activity: Activity,
        str0: String,
        str1: String,
        str2: String,
        str3: String,
        callback: ICallback
    ): Dialog {
        val dialog = Dialog(activity, R.style.CustomProgressDialog)
        dialog!!.setContentView(R.layout.dialog_common)
        dialog!!.window!!.attributes.gravity = Gravity.CENTER
        dialog!!.show()

        val title_view = dialog!!.findViewById<TextView>(R.id.title_view)
        val content_view = dialog!!.findViewById<TextView>(R.id.content_view)
        val btn_cancel = dialog!!.findViewById<TextView>(R.id.btn_cancel)
        val btn_confirm = dialog!!.findViewById<TextView>(R.id.btn_confirm)

        title_view.text = str0
        content_view.text = str1
        btn_cancel.text = str2
        btn_confirm.text = str3
        btn_cancel.setOnClickListener {
            dialog.dismiss()
            callback.onSendEvent(Constants.EventEnum.拒绝.ordinal)
        }
        btn_confirm.setOnClickListener {
            dialog.dismiss()
            callback.onSendEvent(Constants.EventEnum.同意.ordinal)
        }

        return dialog
    }

    /**title_str    content_str   btn_str
     * 单个确定按钮弹窗
     * cancel  true点即弹窗可消失
     */
    fun showSingleBtnDialog(
        activity: Activity,
        str1: String,
        str2: String,
        str3: String, cancel: Boolean,
        callback: com.wink_172.library.callback.ICallback?
    ): Dialog {
        val dialog = Dialog(activity, R.style.CustomProgressDialog)

        dialog!!.setContentView(R.layout.dialog_single_btn2)
        dialog!!.window!!.attributes.gravity = Gravity.CENTER
        dialog!!.setCancelable(cancel)
        dialog!!.setCanceledOnTouchOutside(false)
        dialog!!.show()

        val title_view = dialog!!.findViewById<TextView>(R.id.title_view)
        val content_view = dialog!!.findViewById<TextView>(R.id.content_view)
        val btn_confirm = dialog!!.findViewById<TextView>(R.id.btn_confirm)

        title_view.text = str1
        content_view.text = str2

        btn_confirm.text = str3
        btn_confirm.setOnClickListener(View.OnClickListener {

            dialog.dismiss()
            if (callback != null) {
                callback.onSendEvent(Constants.EventEnum.单个确定按钮点击回调.ordinal)
            }

        })

        return dialog
    }

    /**
     * 只展示标题不展示content
     * 展示普通通用弹窗  左右双按钮
     * str1标题
     * str2左按钮
     * str3右按钮
     */
    fun showCommonDialog(
        activity: Activity,
        str1: String,
        str2: String,
        str3: String,
        callback: ICallback
    ): Dialog {
        val dialog = Dialog(activity, R.style.CustomProgressDialog)
        dialog!!.setContentView(R.layout.dialog_common)
        dialog!!.setCanceledOnTouchOutside(false)
        dialog.setCancelable(false)
        dialog!!.window!!.attributes.gravity = Gravity.CENTER
        dialog!!.show()
        val title_view = dialog!!.findViewById<TextView>(R.id.title_view)
        val content_view = dialog!!.findViewById<TextView>(R.id.content_view)
        val btn_cancel = dialog!!.findViewById<TextView>(R.id.btn_cancel)
        val btn_confirm = dialog!!.findViewById<TextView>(R.id.btn_confirm)



        return dialog
    }

    /**
     * 弹窗1
     */
    fun showRequestStoreDialog(
        activity: Activity,
        type: Int,
        callback: com.wink_172.library.callback.ICallback?
    ) {
        val dialog = Dialog(activity, R.style.CustomProgressDialog)
        dialog!!.setContentView(R.layout.dialog_common)
        dialog!!.window!!.attributes.gravity = Gravity.CENTER
        dialog!!.setCanceledOnTouchOutside(false)
        dialog!!.show()
        val title_view = dialog!!.findViewById<TextView>(R.id.title_view)
        val content_view = dialog!!.findViewById<TextView>(R.id.content_view)
        val btn_cancel = dialog!!.findViewById<TextView>(R.id.btn_cancel)
        val btn_confirm = dialog!!.findViewById<TextView>(R.id.btn_confirm)
        title_view.text = "提示"

        when (type) {
            0 -> {
                content_view.text = "请开启添加照片权限\n" +
                        "否则将无法保存照片"
            }
            1 -> {
                content_view.text = "请开启添加文件读写权限\n" +
                        "否则将无法访问文件"
            }
        }

        btn_cancel.text = "暂不开启"
        btn_confirm.text = "去开启"
        btn_cancel.setOnClickListener {
            dialog.dismiss()


        }
        btn_confirm.setOnClickListener {
            dialog.dismiss()

        }
    }


    /**
     * 在其他应用上层显示下载悬浮布局
     */
    fun showRoomLayout(context: Activity) {


        //宽高设置为屏幕宽度。（游戏存在横屏与竖屏）
        val layoutParams = WindowManager.LayoutParams()
        layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT
        layoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            layoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
        } else {
            layoutParams.type = WindowManager.LayoutParams.TYPE_PHONE
        }
        //效果为背景透明
        layoutParams.format = PixelFormat.RGBA_8888
        layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT
        layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT
        layoutParams.x = -1
        layoutParams.y = -1
        layoutParams.alpha = 0.799f

        // 调整悬浮窗显示的停靠位置为左侧置顶
        layoutParams.gravity = Gravity.START or Gravity.TOP


        // 透明
        layoutParams.flags =
            layoutParams.flags or WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
        layoutParams.flags = layoutParams.flags or WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
        layoutParams.flags = layoutParams.flags or WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
        // 非透明部分不获取交点
        layoutParams.flags = layoutParams.flags or WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
        layoutParams.flags = layoutParams.flags or WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
        layoutParams.flags = layoutParams.flags or WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
//        layoutParams.x = dp2px(context, 20f)
//        layoutParams.y = DisplayUtil.dp2px(context, 20f * 5)


    }

}